/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.gui.documents.toolbox;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import common.Commands;

import ui.WindowManager;
import ui.gui.actions.LoadModelAction;
import ui.gui.actions.OpenFileAction;
import ui.gui.documents.AbstractDocument;
import ui.gui.documents.AltaricaCodeEditor;


/**
 * 
 * @author stephane
 */
public class WorkspaceExplorer extends AbstractToolbox implements MouseListener {
	private static WorkspaceExplorer _instance = null;

	private static final long serialVersionUID = 1L;

	private ExplorerTreeView _treeView = null;

	private TreeModel _treeModel = null;

	private Workspace _workspace = null;

	private JScrollPane _scrollPane = null;

	private JPopupMenu _mnuContext = null;

	private JMenuItem _actionSetCurrentModel = null;
	
	public static WorkspaceExplorer getInstance() {
		if (_instance == null) {
			_instance = new WorkspaceExplorer();
		}
		return _instance;
	}

	public void expandAll() {
		this._treeView.expandAll();
	}

	public void refresh() {
		this._treeView.updateUI();

	}

	protected WorkspaceExplorer() {

		initComponent();
	}

	private void initComponent() {
		setTitle("Workspace Explorer");
		this._workspace = Workspace.getInstance();
		this._treeModel = new DefaultTreeModel(_workspace);
		this._treeView = new ExplorerTreeView(this._treeModel);
		this._treeView.addMouseListener(this);
		this._treeView.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		this._treeView.setCellRenderer(new WorkspaceTreeRender());
		this._treeView.setShowsRootHandles(true);
		this._scrollPane = new JScrollPane(_treeView);
		this._actionSetCurrentModel = new JMenuItem("Set as current model");
		
		this._mnuContext = new JPopupMenu();
		this._mnuContext.add(new LoadModelAction(null, true));
		this._mnuContext.add(new OpenFileAction(null));
		this._mnuContext.add(_actionSetCurrentModel);
		
		
		_actionSetCurrentModel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					DefaultMutableTreeNode node = (DefaultMutableTreeNode) _treeModel
							.getChild(_workspace,
									_treeView.getSelectionRows()[0] - 1);
					if (node != null
							&& !node.isRoot()
							&& node.getUserObject() != null
							&& node.getUserObject() instanceof AltaricaCodeEditor) {
						AltaricaCodeEditor abstractDocument = (AltaricaCodeEditor) node
								.getUserObject();
						Commands.RunCommand("select-model "
								+ abstractDocument.getFileName(), true);
					}
				} catch (Exception ex) {

				}
			}
		});
		
		

		this.setLayout(new BorderLayout());
		this.add(this._scrollPane, BorderLayout.CENTER);
		this.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/resources/workspaceIcon.png")));
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getClickCount() == 2) {
			try {
				int selRow = _treeView.getRowForLocation(e.getX(), e.getY());
				TreePath selPath = _treeView.getPathForLocation(e.getX(), e
						.getY());
				if (selRow != -1) {
					DefaultMutableTreeNode node = null;
					if (selPath.getPathCount() == 2) {
						node = (DefaultMutableTreeNode)selPath.getLastPathComponent();
					}
					else if (selPath.getPathCount() == 3) {
						node = (DefaultMutableTreeNode)selPath.getParentPath().getLastPathComponent();
					}

					if (node != null && !node.isRoot()) {
						AbstractDocument abstractDocument = (AbstractDocument) node
								.getUserObject();
						WindowManager.getInstance().SelectWindow(
								abstractDocument);
					}

				}
			} catch (ArrayIndexOutOfBoundsException ex) {

			}

		} else {
			if (e.getButton() == MouseEvent.BUTTON3) {
				_mnuContext
						.show((JComponent) e.getSource(), e.getX(), e.getY());
			}
		}
	}

	
	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {

	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

}
